package org.dregs.utils;

import com.google.common.collect.Maps;
import org.dregs.pojo.tuple.Tuple;
import org.dregs.pojo.tuple.Tuple2;

import java.util.HashMap;
import java.util.Map;

public class BuildMap<A,B> {

    private HashMap<A,B> hashMap;

    public BuildMap(){
        this.hashMap = new HashMap<A,B>();
    }
    public Map<A,B> toMap(){
        return this.hashMap;
    }
    public BuildMap<A,B> put(A key,B value){
        hashMap.put(key, value);
        return this;
    }

    public BuildMap<A,B> put(Tuple2<A,B> tuple2){
        hashMap.put(tuple2._1(), tuple2._2());
        return this;
    }

    public BuildMap<A,B> put(Map<A,B> map){
        hashMap.putAll(map);
        return this;
    }


    public static <K,V> Map<K,V> asMap(Tuple2<K,V> ...tuple2s){
        HashMap<K, V> hashMap = Maps.newHashMap();
        for (Tuple2<K,V> tuple2 : tuple2s)
            hashMap.put(tuple2._1(),tuple2._2());
        return hashMap;
    }
    public static <K,V> Tuple2<K,V> asTuple2(K key,V value){
        return Tuple.initialize(key,value);
    }

    public static Map<String,Object> newResultMap(){
        return Maps.newHashMap();
    };

    public static BuildMap<String,Object> newBuildMap(){
        return new BuildMap<String,Object>();
    };
    public static <K,V> BuildMap<K,V> newBuildMap(Class<K> kClass , Class<V> vClass){
        return new BuildMap<K,V>();
    };

}